<?php

namespace app\common\controller;

use app\common\library\Auth;
use fast\Http;
use think\Config;
use think\exception\HttpResponseException;
use think\exception\ValidateException;
use think\Hook;
use think\Lang;
use think\Loader;
use think\Request;
use think\Response;
use think\Route;
use think\Validate;

/**
 * API控制器基类
 */
class Wechat
{

    private static $wechatAppId = '';
    private static $wechatSecret = '';
    private static $appletAppId = '';
    private static $appletSecret = '';
    private static $appAppId = '';
    private static $appSecret = '';
    private $access_token = '';
    private $open_id_wechat = '';

    public function __construct()
    {
        $third_config = include(APP_PATH . 'third.php');
        self::$wechatAppId = $third_config['wechat_app_id'];
        self::$wechatSecret = $third_config['wechat_secret'];
        self::$appletAppId = $third_config['applet_app_id'];
        self::$appletSecret = $third_config['applet_secret'];
        self::$appAppId = $third_config['app_app_id'];
        self::$appSecret = $third_config['app_secret'];
    }

    // 获取小程序的openid
    public function getOpenId($code, $type = "wechat")
    {
        switch ($type) {
            case "wechat":
                $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . self::$wechatAppId . '&secret=' . self::$wechatSecret . '&code=' . $code . '&grant_type=authorization_code';
                break;
            case "applet":
                $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . self::$appletAppId . '&secret=' . self::$appletSecret . '&js_code=' . $code . '&grant_type=authorization_code';
                break;
        }

        $ret = json_decode(get_send_data($url), true);
        var_dump($ret);
        die;
        if (isset($ret['errcode'])) {
            return ['code' => 0, 'msg' => $ret['errmsg']];
        }

        return ['code' => 1, 'data' => $ret['openid']];
    }

    /**
     * 网页获取access_token
     * @return array
     */
    public function getAccessToken($code)
    {
        $ret = Http::get("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . self::$wechatAppId . "&secret=" . self::$wechatSecret . "&code=" . $code . "&grant_type=authorization_code");
        $data = json_decode($ret, true);
        if (isset($data['access_token']) && $data['access_token']) {
            $this->access_token = $data['access_token'];
            $this->open_id_wechat = $data['openid'];
            return ['code' => 1];
        } else {
            return ['code' => 0, 'msg' => $data['errmsg']];
        }
    }

    public function getAccessToken1($code)
    {
        $ret = Http::get('https://api.weixin.qq.com/sns/jscode2session?appid=' . self::$appletAppId . '&secret=' . self::$appletSecret . '&js_code=' . $code . '&grant_type=authorization_code');
        $data = json_decode($ret, true);
        var_dump($data);
        die;
        if (isset($data['access_token']) && $data['access_token']) {
            $this->access_token = $data['access_token'];
            return ['code' => 1];
        } else {
            return ['code' => 0, 'msg' => $data['errmsg']];
        }
    }

    /**
     * 获取微信公众号信息
     * @return array
     */
    public function getUserInfo($code)
    {
        $ret1 = self::getAccessToken($code);
        if ($ret1['code'] == 0) {
            return $ret1;
        }
        $ret = Http::get('https://api.weixin.qq.com/sns/userinfo?access_token=' . $this->access_token . '&openid=' . $this->open_id_wechat . '&lang=zh_CN');
        $data = json_decode($ret, true);

        if (isset($data['openid']) && $data['openid']) {
            return ['code' => 1, 'data' => $data];
        } else {
            return ['code' => 0, 'msg' => $data['errmsg']];
        }
    }

    /**
     * 获取session_key
     * @param $code
     */
    public function getSessionKey($code)
    {
        $ret = Http::get("https://api.weixin.qq.com/sns/jscode2session?appid=" . self::$appletAppId . "&secret=" . self::$appletSecret . "&js_code=" . $code . "&grant_type=authorization_code");
        $ret = json_decode($ret, true);
        if (isset($ret['errcode'])) {
            return ['code' => 0, 'msg' => $ret['errmsg']];
        }

        return ['code' => 1, 'data' => ['session_key' => $ret['session_key'], 'open_id' => $ret['openid']]];
    }

    /**
     * 获取小程序的手机号码
     * @param $code
     * @return array
     */
    public function getPhone($code)
    {
        $ret1 = self::getAccessToken1($code);
        if ($ret1['code'] == 0) {
            return $ret1;
        }
        $ret = Http::get('https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=' . $this->access_token);
        $data = json_decode($ret, true);
        if (isset($data['phone_info']['phoneNumber']) && $data['phone_info']['phoneNumber']) {
            return ['code' => 1, 'data' => $data['phone_info']['phoneNumber']];
        } else {
            return ['code' => 0, 'msg' => $data['errmsg']];
        }
    }

    public function getUnionidByOpenid($openID = '')
    {
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="
            . $this->getaccesstoken()
            . "&openid=" . $openID . "&lang=zh_CN";
        $jsonRes = $this->http_get($url);
        if ($jsonRes) {
            $arrRes = json_decode($jsonRes, true);
            return $arrRes['unionid'];
        } else {
            return '';
        }
    }
}
